/*
 * © 2010 Autodata Solutions Company. All Rights Reserved
 * This source code is the confidential and proprietary information of Autodata Solutions Company.
 * The user shall not, in whole or in part, copy, publish, disclose or make any use of this source code
 * without the prior written consent of Autodata Solutions Company.
 */
Ext.ns('net.autodata.modelwalk.data');
(function () {
	
	var keyFunc = function (o) {
		return o.code;
	}
	
	/**
	 * Note this is a private class can cannot be created directly
	 * @class YMMStoreImpl
	 * @extends Ext.util.Observable
	 * @namespace net.autodata.modelwalk.data.impl
	 */
	var YMMStoreImpl = Ext.extend(Ext.util.Observable, {
		/**
		 * @property data
		 * @type Ext.util.MixedCollection
		 */
		data: undefined,
		
		/**
		 * @property isReady
		 * @type bool
		 */
		isReady: false,
		
		/**
		 * @property ajaxCfg
		 * @type Object
		 */
		ajaxCfg: {
			url: String.format('{0}/Ajax/ModelWalk/YMM/', ASC.cfg.getLocaleContextPath()),
			handleAs: 'json',
			method: 'POST'
		},
		
		/**
		 * @property ajax
		 * @type net.autodata.data.JsonReqest
		 */
		ajax: undefined,
		
		constructor: function ()
		{
			this.addEvents({
				/**
				 * @event onready
				 * Fires when the DataStore has been loaded
				 */
				'onready': true,
				
				/**
				 * @event loadexception
				 * Fires when there was an error retrieving the data from the server
				 */
				'loadexception': true
				
			});
			YMMStoreImpl.superclass.constructor.apply(this, arguments);
			
			this.ajax = new net.autodata.data.JsonRequest(this.ajaxCfg);
			this.data = new Ext.util.MixedCollection(false, keyFunc);
			this.data.on('clear', function() {
				this.isReady = false;
			}, this);
		},
		
		/**
		 * Use Ajax to fetch the YYM data
		 * @param {Object} options
		 */
		load: function (options)
		{
			var request = Ext.applyIf({
				success: function (response, ioArgs) {
					this.loadData(response);
				},
				
				failure: function (response, ioArgs) {
					this.fireEvent('loadexception', response.message);
				},
				
				scope: this
				
			}, options);
		
			this.ajax.request(request);
		},
		
		/**
		 * Load the YYMDataStore from a local dataset
		 * @param {Array} ymmData
		 */
		loadData: function (ymmData)
		{
			var data = this.data;
			data.clear();
			
			//Process the yym tree data into a collection
			Ext.each(ymmData, function (modelYear) {
				
				var makes = new Ext.util.MixedCollection(false, keyFunc);
				Ext.each(modelYear.makes, function (make) {
					
					var models = new Ext.util.MixedCollection(false, keyFunc);					
					models.addAll(make.models);
					
					makes.add({code: make.code, description: make.description, models: models});
					
				}, this);
				
				data.add({code: modelYear.code, description: modelYear.description, makes: makes});
				
			}, this);
		
			this.isReady = true;
			this.fireEvent('onready');
		},
		
		/**
		 * @method getModelYears
		 * @returns Array<net.autodata.modelwalk.ModelYear>
		 */
		getModelYears: function ()
		{
			return this.data.getRange();
		},
		
		/**
		 * @method getMakes
		 * @param {String} modelYearCode
		 * @returns Array<net.autodata.modelwalk.Make>
		 */
		getMakes: function (modelYearCode)
		{
			var modelYear = this.data.get(modelYearCode);
			return modelYear ? modelYear.makes.getRange() : [];
		},
		
		/**
		 * @method getModels
		 * @param {String} modelYearCode
		 * @param {String} makeCode
		 * @returns Array<net.autodata.modelwalk.Model>
		 */
		getModels: function (modelYearCode, makeCode)
		{
			var modelYear = this.data.get(modelYearCode);
			if (modelYear) {
				var make = modelYear.makes.get(makeCode);
				if (make) {
					return make.models.getRange();
				}
			}
			
			return [];
		}
	});
	
	var ymmDataStore = new YMMStoreImpl();
	
	/**
	 * @class YearMakeModelDataStore
	 * @singleton
	 * @namespace net.autodata.modelwalk.data
	 */
	var DataStore = {
		/**
		 * @method getInstance
		 * @returns net.autodata.modelwalk.data.impl.YMMStoreImpl
		 */
		getInstance: function ()
		{
			return ymmDataStore;
		}
	};
	
	net.autodata.modelwalk.data.YearMakeModelDataStore = DataStore;
})();